package file

import (
	"errors"
	"os"
)

// Open 打开文件，返回抽象的 File 接口
func Open(path string, mode string) (File, error) {
	var flags int
	switch mode {
	case "r":
		flags = os.O_RDONLY
	case "w":
		flags = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
	case "a":
		flags = os.O_WRONLY | os.O_CREATE | os.O_APPEND
	default:
		return nil, errors.New("unsupported mode: " + mode)
	}

	file, err := os.OpenFile(path, flags, 0666)
	if err != nil {
		return nil, err
	}

	return &LocalFile{file: file}, nil
}
